package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.ReggieException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);
        if (!dishList.isEmpty()) {
            JSONArray dishArray = dishList.stream().map(dish -> {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", dish.getName());
                        jsonObject.put("id", dish.getId());
                        return jsonObject;
                    })
                    .collect(Collectors.toCollection(JSONArray::new));
            log.error("delete category :{} with dish list :{}", id, JSONArray.toJSONString(dishArray, SerializerFeature.PrettyFormat));
            throw new ReggieException("delete category with dish related");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        if (!setmealList.isEmpty()) {
            JSONArray setmealArray = setmealList.stream().map(setmeal -> {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", setmeal.getName());
                        jsonObject.put("id", setmeal.getId());
                        return jsonObject;
                    })
                    .collect(Collectors.toCollection(JSONArray::new));
            log.error("delete category :{} with setmeal list :{}", id, JSONArray.toJSONString(setmealArray, SerializerFeature.PrettyFormat));
            throw new ReggieException("delete category with setmeal related");
        }

        super.removeById(id);
    }
}
